package com.zwh.spring.demo.mapper.primary;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.zwh.spring.demo.domain.User;

@Mapper
public interface UserMapper {
	
	@Select("Select * from User where name = #{name}")
	User findByName(@Param("name") String name);
	
	@Results({
	    @Result(property = "name", column = "name"),
	    @Result(property = "password", column = "password"),
	    @Result(property = "age", column = "age")
	})
	@Select("SELECT name,password, age FROM user")
	List<User> findAll();
	
	/**
	 * 使用Param作为参数
	 * @param name
	 * @param password
	 * @param age
	 * @return
	 */
	@Insert("Insert into User(name, password, age) values(#{name}, #{password}, #{age})")
	Integer insertByParam(@Param("name") String name, @Param("password") String password, @Param("age") Integer age);
	
	/**
	 * 使用Map作为参数
	 * 
	 * map.put("name", "tom")
	 * map.put("password", "123456")
	 * map.put("age", 100)
	 * 
	 * @param map
	 * @return
	 */
	@Insert("Insert into User(name, password, age) values(#{name}, #{password}, #{age})")
	Integer insertByMap(Map<String, Object> map);
	
	/**
	 * 使用对象作为参数
	 * 
	 * new User("tom", "123456", 100)
	 * 
	 * @param user
	 * @return
	 */
	@Insert("Insert into User(name, password, age) values(#{name}, #{password}, #{age})")
	Integer insertByUser(User user);

	@Delete("Delete from User")
	void deleteAll();

	@Select("Select count(1) from user ")
	Integer count();
}
